// page/cart/address.js
var util = require("../../util/util.js");
var address = require('../../util/city.js')
var animation
Page({
  data: {
    name:'',
    phone: '',
    city: '',
    xxdz: '',
    id:'',
    isHide: false,
    lx:'添加地址',

    animationAddressMenu: {},
    addressMenuIsShow: false,
    value: [0, 0, 0],
    provinces: [],
    citys: [],
    areas: [],
    province: '',
    icity: '',
    area: ''      
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.id){
      var app = getApp()
      var uid = wx.getStorageSync('uid');
      wx.request({
        url: app.globalData.host + '&g=App&m=Cart&a=savechoose', //仅为示例，并非真实的接口地址
        data: { id: options.id },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          // console.log(res.data)
          that.setData({
            id: options.id,
            name: res.data.name,
            phone: res.data.phone,
            city: res.data.city,
            xxdz: res.data.xxdz,
            lx: '修改',
            isHide: 1,
          })
        }
      }); 
    }
   
    
    that.setData({
        isHide: true
    })   

    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 500,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
    // 默认联动显示北京
    var id = address.provinces[0].id
    that.setData({
      provinces: address.provinces,
      citys: address.citys[id],
      areas: address.areas[address.citys[id][0].id],
    })       
  },

  /**
   * 表单验证
   */
  formSubmit: function (e) {
      var uid = wx.getStorageSync('uid');
      var username = e.detail.value.username
      var phone = e.detail.value.phone
      var city =this.data.city
      var detail = e.detail.value.detail
      if (username.length < 1) {
          util.showToptips("请输入收货人姓名", 2000, function () { });
          return;
      } else if (phone.length < 1) {
          util.showToptips("请输入收货人手机", 2000, function () { });
          return;
      } else if (city.length < 1) {
        util.showToptips("请输入省市区", 2000, function () { });
          return;
      } else if (detail.length < 1) {
          util.showToptips("请输入收货人详细地址", 2000, function () { });
          return;
      } else {
        var app = getApp()
        // console.log(app.globalData.host)
        var that = this;
        if(that.data.lx=='修改'){
          wx.request({
            url: app.globalData.host + '&g=App&m=Cart&a=savecity', //仅为示例，并非真实的接口地址
            data: {
              id: that.data.id, city: city, phone: phone, address: detail, name: username
            },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
               
                wx.navigateBack();
              
            }
          })
        }else{
        wx.request({
          url: app.globalData.host + '&g=App&m=Cart&a=addcity', //仅为示例，并非真实的接口地址
          data: {
            uid: uid, city: city, phone: phone, address: detail,name:username
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res.data.count)
            if(res.data.id){
              wx.setStorageSync('addressid', res.data.id)
              wx.redirectTo({
                url: 'choose'
              })
            }
          }
        })
        }  
      }
  },
  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    if (that.data.addressMenuIsShow) {
      return
    }
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    var that = this
    if (isShow) {
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(40 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  // 点击地区选择取消按钮
  cityCancel: function (e) {
    this.startAddressAnimation(false)
  },
  // 点击地区选择确定按钮
  citySure: function (e) {
    var that = this
    var city = that.data.icity
    var value = that.data.value
    that.startAddressAnimation(false)
    // 将选择的城市信息显示到输入框
    var areaInfo = that.data.provinces[value[0]].name + '-' + that.data.citys[value[1]].name + '-' + that.data.areas[value[2]].name
    that.setData({
      city: areaInfo,
    })
  },
  hideCitySelected: function (e) {
    this.startAddressAnimation(false)
  },
  // 处理省市县联动逻辑
  cityChange: function (e) {
    var value = e.detail.value
    var provinces = this.data.provinces
    var citys = this.data.citys
    var areas = this.data.areas
    var provinceNum = value[0]
    var cityNum = value[1]
    var countyNum = value[2]
    if (this.data.value[0] != provinceNum) {
      var id = provinces[provinceNum].id
      this.setData({
        value: [provinceNum, 0, 0],
        citys: address.citys[id],
        areas: address.areas[address.citys[id][0].id],
      })
    } else if (this.data.value[1] != cityNum) {
      var id = citys[cityNum].id
      this.setData({
        value: [provinceNum, cityNum, 0],
        areas: address.areas[citys[cityNum].id],
      })
    } else {
      this.setData({
        value: [provinceNum, cityNum, countyNum]
      })
    }
  }    
})